package net.libra.loader
{
	import com.cw.war.ui.LoadingBox;
	
	/**
	 * loadUtil
	 * 加载完一个swf，然后执行某个函数
	 * @author elvisjiang
	 */	
	public class LoadUtil
	{
		private static var _callBack:Function;
		private static var _context:Object;
		private static var _parms:Array;
		private static var _swfloader:SwfLoader;
		
		/**
		 * @param libs 加载的swf
		 * @param callBack 回调函数
		 * @param context 函数所属对象
		 * @param parms 函数参数列表
		 */		
		public static function load(libs:Array, callBack:Function=null, context:Object=null, parms:Array=null):void
		{
			_callBack = callBack;
			_context = context;
			_parms = parms;
			if(SwfLoader.isLoaded(libs))
			{
				onCompleteHandler();
			}
			else
			{
				_swfloader = new SwfLoader();
				_swfloader.addURLArray(libs);
				_swfloader.addEventListener(LoadEvent.ON_PROGRESS, onProgressHandler);
				_swfloader.addEventListener(LoadEvent.ON_COMPLETE, onCompleteHandler);
				_swfloader.start();
				LoadingBox.show();
			}	
		}
		
		/**
		 * 响应加载进度事件
		 * */
		private static function onProgressHandler(event:LoadEvent):void
		{
			LoadingBox.updateState(event.data);
		}
		
		/**
		 * 响应加载完成事件
		 * */
		private static function onCompleteHandler(event:LoadEvent=null):void
		{
			if(_swfloader)
			{
				_swfloader.removeEventListener(LoadEvent.ON_PROGRESS, onProgressHandler);
				_swfloader.removeEventListener(LoadEvent.ON_COMPLETE, onCompleteHandler);
				_swfloader = null;
			}
			LoadingBox.remove();
			if(_callBack != null)
			{
				_callBack.apply(_context, _parms);
				_callBack = null;
			}
			_context = null;
			_parms.length = 0;
		}
		
	}
}